﻿using System;
using System.Drawing;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary;
using CommonLibraries.DeviceLibrary.Helpers;

namespace DistributionApplication
{
    public partial class frmTest : Form
    {
        private readonly GPS GPSInstance;

        public frmTest()
        {
            InitializeComponent();

            GPSInstance = GPS.GetInstance();

            GPSOn();

            GPSInstance.HasSolutionChanged += m_GPS_HasSolutionChanged;
        }

        protected void GPSOn()
        {
            if (GPSInstance.PowerState == PowerStates.Off)
            {
                GPSInstance.Reset();
                GPSInstance.PowerState = PowerStates.On;
            }
        }

        public void m_GPS_HasSolutionChanged(object sender, EventArgs e)
        {
            try
            {
                Invoke(new ThreadDelegate(this.HasSolutionChanged));
            }
            catch (ObjectDisposedException ex)
            {
                ErrorManager.CreateDebug("BeginInvoke->" + ex.Message + "/n/r" + ex.StackTrace);
            }
        }

        private delegate void SolutionChangedCallback();
        private delegate void ThreadDelegate();

        protected void HasSolutionChanged()
        {
            if (InvokeRequired)
            {
                SolutionChangedCallback d = HasSolutionChanged;
                Invoke(d);
            }
            else
            {
                try
                {
                    GPSOn();

                    semaphore.BackColor = GPSInstance.HasSolution ? Color.Green : Color.Red;
                    Quality.Text = GPSInstance.NmeaGGA.Quality.ToString();
                    Satellites.Text = GPSInstance.NmeaGGA.SateliteCount.ToString();

                    if (GPSInstance.HasSolution) {
                        Latitude.Text = GPSInstance.NmeaGGA.Latitude;
                        Longitude.Text = GPSInstance.NmeaGGA.Longitude;
                    }
                    else
                    {
                        Latitude.Text = "-";
                        Longitude.Text = "-";
                    }
                }
                catch (Exception ex)
                {
                    ErrorManager.CreateDebug("HasSolutionChanged->Problems\n" + ex.Message);
                }
            }
        }

        private void Longitude_ParentChanged(object sender, EventArgs e)
        {

        }

        private void Latitude_ParentChanged(object sender, EventArgs e)
        {

        }

        private void semaphore_ParentChanged(object sender, EventArgs e)
        {

        }

        private void Quality_ParentChanged(object sender, EventArgs e)
        {

        }

        private void rdbAMode_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbAMode.Checked)
                GPSInstance.ChangeToMode(ReceptionMode.AMode);
        }

        private void rdbBMode_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbBMode.Checked)
                GPSInstance.ChangeToMode(ReceptionMode.BMode);
        }
    }
}